import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:yt_devops/components/brn/brn_select_tag.dart';
import 'package:yt_devops/pages/devices/device_list/components/filter_dialog.dart';
import 'package:yt_devops/resources/str_res_keys.dart';
import 'package:yt_devops/routers/router_path.dart';
import 'package:yt_devops/styles/styles.dart';
import 'package:yt_devops/utils/func_utils.dart';

class ProductTypeEnity {
  final String name;
  final num id;
  final ProductTypeEnity? subProduct;

  const ProductTypeEnity(
      {required this.name, required this.id, this.subProduct});

  factory ProductTypeEnity.fromJson(Map<String, dynamic> json) {
    return ProductTypeEnity(
        name: json['name'].toString(),
        id: json['id'] is int ? json['id'] : int.tryParse(json['id']),
        subProduct: json['subProduct'] != null
            ? ProductTypeEnity.fromJson(json['subProduct'])
            : null);
  }

  Map<String, dynamic> toJson() => {
        'name': name,
        'id': id,
        'subProduct': subProduct?.toJson(),
      };
}

class FilterProductTags extends StatefulWidget {
  ProductTypeEnity? productTypeEnity;

  final Function(ProductTypeEnity?)? onSelect;
  FilterProductTags(this.productTypeEnity, {this.onSelect, super.key});

  @override
  State<FilterProductTags> createState() => _FilterProductTagsState();
}

class _FilterProductTagsState extends State<FilterProductTags> {
  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      onTap: () {
        Get.toNamed(RouterPath.selectProductType,
                arguments: widget.productTypeEnity?.toJson())
            ?.then((value) {
          if (value is Map<String, dynamic>) {
            if (value['id'] == null) {
              widget.productTypeEnity = null;
            } else {
              widget.productTypeEnity = ProductTypeEnity.fromJson(value);
            }
            setState(() {
              widget.onSelect?.call(widget.productTypeEnity);
            });
          }
        });
      },
      child: Column(crossAxisAlignment: CrossAxisAlignment.start, children: [
        FilterDialogWidget.pickTitle(ResKey.deviceType.tr),
        Container(
          alignment: Alignment.center,
          decoration: BoxDecoration(
            color: tagBackgroundColor,
            borderRadius: BorderRadius.circular(4),
          ),
          width: tagWidth,
          height: 35.h,
          child: Text(
            text,
            style: TextStyles.pf12,
          ),
        ),
      ]),
    );
  }

  String get text => widget.productTypeEnity == null
      ? ResKey.pleSelect.tr
      : widget.productTypeEnity?.subProduct == null
          ? '${widget.productTypeEnity?.name}'
          : '${widget.productTypeEnity?.name}/${widget.productTypeEnity?.subProduct?.name}';

  Color get tagBackgroundColor => widget.productTypeEnity == null
      ? Colour.backgroundColor
      : Colour.primaryColor.withOpacity(0.1);

  double get tagWidth {
    if (widget.productTypeEnity == null) {
      return 104.w;
    } else if (widget.productTypeEnity?.subProduct != null) {
      return double.infinity;
    } else if (widget.productTypeEnity?.subProduct != null &&
        FuncUtils.iszhCN()) {
      return 161.w;
    } else {
      return 101.w;
    }
  }

  int? get singleSelectIndex => widget.productTypeEnity == null ? null : 0;
}
